package Eyjafjalla.patch;

import Eyjafjalla.modcore.EyjafjallaTag;
import Eyjafjalla.modcore.OtherEnum;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpirePostfixPatch;
import com.megacrit.cardcrawl.actions.defect.DiscardPileToHandAction;
import com.megacrit.cardcrawl.actions.utility.DiscardToHandAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;

import java.util.ArrayList;

public class CloudFixPatch {

    //第一回合开始后才可以
    public static boolean canSetRoundEnd = false;
    public static ArrayList<AbstractCard> lovedThisTurn = new ArrayList<>();
    public static ArrayList<AbstractCard> glassedThisTurn = new ArrayList<>();

    public static void turnClear(){
        lovedThisTurn.clear();
        glassedThisTurn.clear();
    }

    //本Patch是用来使得云层牌进入其他牌堆后失去云层属性

    @SpirePatch(clz = CardGroup.class,method = "addToTop")
    public static class CGAddToTopPatch{
        @SpirePostfixPatch
        public static void Postfix(CardGroup _inst, AbstractCard c){
            if(_inst.type== CardGroup.CardGroupType.DISCARD_PILE){
                //TheLovedOne
                if(c.hasTag(EyjafjallaTag.LOVED_EYJAFJALLA)&&!lovedThisTurn.contains(c)){
                    lovedThisTurn.add(c);
                    AbstractDungeon.actionManager.addToBottom(new DiscardToHandAction(c));
                }
            }
            if(!CloudPatch.CloudField.inCloud.get(c))
                return;
            if(_inst.type!= CardGroup.CardGroupType.DRAW_PILE&&_inst.type!= CardGroup.CardGroupType.HAND&&_inst.type!= CardGroup.CardGroupType.DISCARD_PILE&&_inst.type!= CardGroup.CardGroupType.EXHAUST_PILE)
                return;
            CloudPatch.CloudField.inCloud.set(c,false);
        }
    }

    @SpirePatch(clz = CardGroup.class,method = "addToBottom")
    public static class CGAddToBottomPatch{
        @SpirePostfixPatch
        public static void Postfix(CardGroup _inst,AbstractCard c){
            if(_inst.type== CardGroup.CardGroupType.DISCARD_PILE){
                //TheLovedOne
                if(c.hasTag(EyjafjallaTag.LOVED_EYJAFJALLA)&&!lovedThisTurn.contains(c)){
                    lovedThisTurn.add(c);
                    AbstractDungeon.actionManager.addToBottom(new DiscardToHandAction(c));
                }
            }
            if(!CloudPatch.CloudField.inCloud.get(c))
                return;
            if(_inst.type!= CardGroup.CardGroupType.DRAW_PILE&&_inst.type!= CardGroup.CardGroupType.HAND&&_inst.type!= CardGroup.CardGroupType.DISCARD_PILE&&_inst.type!= CardGroup.CardGroupType.EXHAUST_PILE)
                return;
            CloudPatch.CloudField.inCloud.set(c,false);
        }
    }

    @SpirePatch(clz = CardGroup.class,method = "addToRandomSpot")
    public static class CGAddToRandomSpotPatch{
        @SpirePostfixPatch
        public static void Postfix(CardGroup _inst,AbstractCard c){
            if(_inst.type== CardGroup.CardGroupType.DISCARD_PILE){
                //TheLovedOne
                if(c.hasTag(EyjafjallaTag.LOVED_EYJAFJALLA)&&!lovedThisTurn.contains(c)){
                    lovedThisTurn.add(c);
                    AbstractDungeon.actionManager.addToBottom(new DiscardToHandAction(c));
                }
            }
            if(!CloudPatch.CloudField.inCloud.get(c))
                return;
            if(_inst.type!= CardGroup.CardGroupType.DRAW_PILE&&_inst.type!= CardGroup.CardGroupType.HAND&&_inst.type!= CardGroup.CardGroupType.DISCARD_PILE&&_inst.type!= CardGroup.CardGroupType.EXHAUST_PILE)
                return;
            CloudPatch.CloudField.inCloud.set(c,false);
        }
    }

    @SpirePatch(clz = CardGroup.class,method = "addToHand")
    public static class CGAddToHandPatch{
        @SpirePostfixPatch
        public static void Postfix(CardGroup _inst,AbstractCard c){
            if(CloudPatch.CloudField.inCloud.get(c))
                CloudPatch.CloudField.inCloud.set(c,false);
        }
    }
}
